package city.baidi;


import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

import java.math.BigDecimal;
import java.net.HttpCookie;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * projectName:Default (Template) Project
 * city.baidi
 *
 * @author Bruce Maa
 * @since 2024-10-15.21:55
 */
public class Main {
    public static Map<String, String> ssqTypeMap = new HashMap<>();
    public static Map<String, String> kl8TypeMap = new HashMap<>();
    static {
        ssqTypeMap.put("1", "一等奖");
        ssqTypeMap.put("2", "二等奖");
        ssqTypeMap.put("3", "三等奖");
        ssqTypeMap.put("4", "四等奖");
        ssqTypeMap.put("5", "五等奖");
        ssqTypeMap.put("6", "六等奖");
        ssqTypeMap.put("7", "七等奖");
        kl8TypeMap.put("x10z10", "选十中十");
        kl8TypeMap.put("x10z9", "选十中九");
        kl8TypeMap.put("x10z8", "选十中八");
        kl8TypeMap.put("x10z7", "选十中七");
        kl8TypeMap.put("x10z6", "选十中六");
        kl8TypeMap.put("x10z5", "选十中五");
        kl8TypeMap.put("x10z0", "选十中零");
        kl8TypeMap.put("x9z9", "选九中九");
        kl8TypeMap.put("x9z8", "选九中八");
        kl8TypeMap.put("x9z7", "选九中七");
        kl8TypeMap.put("x9z6", "选九中六");
        kl8TypeMap.put("x9z5", "选九中五");
        kl8TypeMap.put("x9z4", "选九中四");
        kl8TypeMap.put("x9z0", "选九中零");
        kl8TypeMap.put("x8z8", "选八中八");
        kl8TypeMap.put("x8z7", "选八中七");
        kl8TypeMap.put("x8z6", "选八中六");
        kl8TypeMap.put("x8z5", "选八中五");
        kl8TypeMap.put("x8z4", "选八中四");
        kl8TypeMap.put("x8z0", "选八中零");
        kl8TypeMap.put("x7z7", "选七中七");
        kl8TypeMap.put("x7z6", "选七中六");
        kl8TypeMap.put("x7z5", "选七中五");
        kl8TypeMap.put("x7z4", "选七中四");
        kl8TypeMap.put("x7z0", "选七中零");
        kl8TypeMap.put("x6z6", "选六中六");
        kl8TypeMap.put("x6z5", "选六中五");
        kl8TypeMap.put("x6z4", "选六中四");
        kl8TypeMap.put("x6z3", "选六中三");
        kl8TypeMap.put("x5z5", "选五中五");
        kl8TypeMap.put("x5z4", "选五中四");
        kl8TypeMap.put("x5z3", "选五中三");
        kl8TypeMap.put("x4z4", "选四中四");
        kl8TypeMap.put("x4z3", "选四中三");
        kl8TypeMap.put("x4z2", "选四中二");
        kl8TypeMap.put("x3z3", "选三中三");
        kl8TypeMap.put("x3z2", "选三中二");
        kl8TypeMap.put("x2z2", "选二中二");
        kl8TypeMap.put("x1z1", "选一中一");
    }
    public static void main(String[] args) throws SQLException {
//        cwl("ssq", ssqTypeMap);
//        cwl("kl8", kl8TypeMap);
//        cwl("3d", ssqTypeMap);
//        cwl("qlc", ssqTypeMap);
        sporttery("85", 1);
        sporttery("35", 71);
        sporttery("350133", 71);
        sporttery("04", 31);
    }

    public static void cwl(String code, Map<String, String> typeMap) throws SQLException {
        String url = "https://www.cwl.gov.cn/cwl_admin/front/cwlkj/search/kjxx/findDrawNotice";
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("name", code);
        //paramMap.put("issueCount", "10");
        paramMap.put("pageNo", "1");
        paramMap.put("pageSize", "5000");
        //paramMap.put("pageSize", "1");
        paramMap.put("systemType", "PC");
        url = url + "?" + HttpUtil.toParams(paramMap);
        System.out.println("url = " + url);
        final HttpRequest httpRequest = HttpUtil.createGet(url);
        HttpResponse httpResponse = httpRequest.execute();
        if (httpResponse.getStatus() == 302) {
            final List<HttpCookie> cookies = httpResponse.getCookies();
            cookies.forEach(System.out::println);
            httpRequest.cookie(cookies);
            httpResponse = httpRequest.execute();
        }
        final String body = httpResponse.body();
        final JSONObject jsonObject = JSONUtil.parseObj(body);
        if (jsonObject.getInt("state") == 0) {
            System.out.println("查询成功");
            final Db db = Db.use();
            final JSONArray result = jsonObject.getJSONArray("result");
            for (int i = result.size() - 1; i >= 0; i--) {
                final JSONObject term = result.getJSONObject(i);
                System.out.println(term);
                final String termNum = term.getStr("code");
                //final Entity entity = db.queryOne("select * from lottery.lottery_history where code = '3d' and term_num =? limit 1", termNum);
                //if (entity != null) {
                //    continue;
                //}
                db.insert(Entity.create("lottery_history")
                        .set("category", "cwl")
                        .set("code", code)
                        .set("name", term.getStr("name"))
                        .set("term_num", termNum)
                        .set("open_date", StrUtil.sub(term.getStr("date"), 0, 10))
                        .set("result_num", String.join(", ", term.getStr("red"), term.getStr("blue")))
                        .set("total_sale_amt", term.getBigDecimal("sales", BigDecimal.ZERO))
                        .set("pool_balance", term.getBigDecimal("poolmoney", BigDecimal.ZERO))
                        .set("content", term.getStr("content"))
                );
                final JSONArray prizegrades = term.getJSONArray("prizegrades");
                for (int i1 = 0; i1 < prizegrades.size(); i1++) {
                    final JSONObject prize = prizegrades.getJSONObject(i1);
                    final String type = prize.getStr("type");
                    if (StrUtil.isBlank(type)) {
                        continue;
                    }
                    db.insert(Entity.create("lottery_history_detail")
                            .set("category", "cwl")
                            .set("code", code)
                            .set("name", term.getStr("name"))
                            .set("term_num", termNum)
                            .set("prize_level", typeMap.get(type))
                            .set("stake_count", prize.getInt("typenum", 0))
                            .set("stake_amt", prize.getBigDecimal("typemoney", BigDecimal.ZERO))
                            .set("total_prize_amt", NumberUtil.mul(prize.getInt("typenum", 0), prize.getBigDecimal("typemoney", BigDecimal.ZERO)))
                            .set("sort", i1)
                    );
                }
            }
        }
    }

    public static void sporttery(String code, int pageNo) throws SQLException {
        String baseUrl = "https://webapi.sporttery.cn/gateway/lottery/getHistoryPageListV1.qry";
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("gameNo", code);
        paramMap.put("provinceId", "0");
        paramMap.put("isVerify", "1");
        int pages = 0;
        paramMap.put("pageSize", "100");
        //paramMap.put("termLimits", "10");
        do {
            paramMap.put("pageNo", pageNo + "");
            String url = baseUrl + "?" + HttpUtil.toParams(paramMap);
            System.out.println("url = " + url);
            final HttpRequest httpRequest = HttpUtil.createGet(url);
            HttpResponse httpResponse = httpRequest.execute();
            final String body = httpResponse.body();
            final JSONObject jsonObject = JSONUtil.parseObj(body);
            final JSONObject value = jsonObject.getJSONObject("value");
            pages = value.getInt("pages");
            System.out.println("查询成功");
            final Db db = Db.use();
            final JSONArray result = value.getJSONArray("list");
            for (int i = result.size() - 1; i >= 0; i--) {
                final JSONObject term = result.getJSONObject(i);
                System.out.println(term);
                final String termNum = term.getStr("lotteryDrawNum");
                //final Entity entity = db.queryOne("select * from lottery.lottery_history where code = '85' and term_num =? limit 1", termNum);
                //if (entity != null) {
                //    continue;
                //}
                db.insert(Entity.create("lottery_history")
                        .set("category", "sporttery")
                        .set("code", code)
                        .set("name", term.getStr("lotteryGameName"))
                        .set("term_num", termNum)
                        .set("open_date", term.getStr("lotteryDrawTime"))
                        .set("result_num", term.getStr("lotteryDrawResult"))
                        .set("total_sale_amt", term.getBigDecimal("totalSaleAmount", BigDecimal.ZERO))
                        .set("pool_balance", term.getBigDecimal("poolBalanceAfterdraw", BigDecimal.ZERO))
                        .set("content", term.getStr("content"))
                        .set("detail_pdf_url", term.getStr("drawPdfUrl"))
                );
                final JSONArray prizegrades = term.getJSONArray("prizeLevelList");
                for (int i1 = 0; i1 < prizegrades.size(); i1++) {
                    final JSONObject prize = prizegrades.getJSONObject(i1);
                    final String prizeLevel = prize.getStr("prizeLevel");
                    if (StrUtil.isBlank(prizeLevel)) {
                        continue;
                    }
                    db.insert(Entity.create("lottery_history_detail")
                            .set("category", "sporttery")
                            .set("code", code)
                            .set("name", term.getStr("lotteryGameName"))
                            .set("term_num", termNum)
                            .set("prize_level", prizeLevel)
                            .set("stake_count", prize.getInt("stakeCount", 0))
                            .set("stake_amt", prize.getBigDecimal("stakeAmount", BigDecimal.ZERO))
                            .set("total_prize_amt", prize.getBigDecimal("totalPrizeamount", BigDecimal.ZERO))
                            .set("sort", prize.getInt("sort"))
                    );
                }
            }
        } while (--pageNo > 0);
    }
}